package com.education.myoschinatest.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.education.myoschinatest.MyApp;
import com.education.myoschinatest.R;
import com.education.myoschinatest.bean.DataBean;
import com.education.myoschinatest.bean.RoomBean;
import com.education.myoschinatest.http.HttpUtils;
import com.education.myoschinatest.http.MyHttpBody;
import com.education.myoschinatest.ui.home.HomeFragment;
import com.education.myoschinatest.ui.vertical.VerticalActivity;
import com.orhanobut.logger.Logger;

import java.util.List;

import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;
    private Button mButton;
    private EditText mEditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton= (Button) findViewById(R.id.button_javatojs);
        mEditText= (EditText) findViewById(R.id.edittext);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //访问网络 返回的是流 读取流 得到json字符串  解析json 得到对象 设置到界面


                Observable.create(new Observable.OnSubscribe<String>(){

                    @Override
                    public void call(Subscriber<? super String> subscriber) {
                        //此处进行耗时操作
                        //

                    }
                }).subscribeOn(Schedulers.newThread())
                        .flatMap(new Func1<String, Observable<DataBean>>() {
                            @Override
                            public Observable<DataBean> call(String s) {
                                return Observable.create(new Observable.OnSubscribe<DataBean>() {
                                    @Override
                                    public void call(Subscriber<? super DataBean> subscriber) {
                                        //使用此s
                                        DataBean bean=new DataBean();
                                        subscriber.onNext(bean);
                                    }
                                });
                            }
                        })

                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<DataBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(DataBean dataBean) {

                            }
                        });


            }
        });
    }
//
//    private void toJs(String url) {
//        //java调用js方法
//        mWebView.loadUrl(url);
//    }
//
//    private void initWebView() {
//        mWebView= (WebView) findViewById(R.id.webview);
//        //在应用内加载网页不跳转到浏览器
//        mWebView.setWebViewClient(new WebViewClient(){
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//        });
//        //为了支持所有的机型 java调用js方法成功,不然js中的alert不支持
//        mWebView.setWebChromeClient(new WebChromeClient());
//        //网页内支持js方法
//        mWebView.getSettings().setJavaScriptEnabled(true);
//        //建立js和java的联系,他们的连接使用的是一个对象
//        WebBridge bridge=new WebBridge();
//        //在android的4.2之前存在js注入的漏洞,android4.之后js调用java方法要求该java方法一个注解
//        mWebView.addJavascriptInterface(bridge,"jsbridge");
//        mWebView.loadUrl("http://10.0.164.76:8080/MyJsonFileTest/helloworld.html");
//    }
//
//    class WebBridge{
//        @JavascriptInterface
//        public void jsToJava(String content){
//            //android中跳转界面的方法
//            Toast.makeText(MainActivity.this, "js调用java方法"+content, Toast.LENGTH_SHORT).show();
////            startActivity(new Intent(getApplicationContext(),VerticalActivity.class));
//        }
//    }

}